//
//  YTUserAccount.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/19.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTUserAccount.h"
NSDateFormatter *dateFmt;


@implementation YTUserAccount
- (NSString *)yy_modelDescription
{
    return [self yy_modelDescription];
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"decs":@"description"};
}

- (void)setBirthday:(NSString *)birthday
{
    dateFmt = [[NSDateFormatter alloc] init];
    dateFmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [dateFmt dateFromString:birthday];
    dateFmt.dateFormat = @"yyyy-MM-dd";
    _birthday = [dateFmt stringFromDate:date];
}

- (void)setAccessToken:(NSString *)accessToken
{
    _accessToken = accessToken;
    
    [[YTNetworkManager.shareManager requestSerializer] setValue:accessToken
                                             forHTTPHeaderField:@"access-token"];
}


- (void)setIsSign:(NSNumber *)isSign
{
    _isSign = isSign;
    
    YTMainViewController *mainVC = (YTMainViewController *)UIApplication.sharedApplication.keyWindow.rootViewController;
    isSign.boolValue ? [mainVC.tabBar hideBadgeOnItemIndex:2] : [mainVC.tabBar showBadgeOnItmIndex:2 tabbarItemNums:3];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        //取出json数据
        NSString *docPath = [YTUserAccountJson dh_appendDocumentDir];
        NSData *data = [NSData dataWithContentsOfFile:docPath];
        
        
        if (!data) {
            return self;
        }
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        //字典转模型
        [self yy_modelSetWithJSON:dict];
    }
    return self;
}

// 保存 用户账户模型 到 NSUserDefaults
- (void)saveUserAccount
{
    //获取doc路径
    NSString *docPath = [YTUserAccountJson dh_appendDocumentDir];
    
    //模型转字典
    NSMutableDictionary *dict = [self yy_modelToJSONObject];
    
    //json反序列化
    NSData *data = [dict yy_modelToJSONData];
    
    //写入磁盘
    [data writeToFile:docPath atomically:true] ? nil : NSLog(@"写入失败");
}
@end
